from annoying.decorators import render_to
from django.shortcuts import redirect
from django.contrib import messages

from core.extensions.decorators import login_required

from core.forms import EventForm
from core.models import User
from core.views.event.join import join


@login_required()
@render_to('event/create.html')
def create(request):
    if request.method == 'POST':
        form = EventForm(request.POST, request.FILES)
        if form.is_valid():
            event = form.save(commit=False)
            event.owner = User.objects.get(id=request.session['user_id'])
            event.save()
            messages.info(request, 'join_after_creation')
            return redirect(join, event.id)
    else:
        form = EventForm()
    return {'form': form}
